Page({
  data:{
    backUrl:'../../images/img.png',  //爆炸图
    desUrl:"../../images/icon.png",  //目标位置
  
    touch: {
      offsetX: 0,
      offsetY: 0,
      zoom: false, //是否缩放状态
      distance: 0,  //两指距离
      scale: 1,  //缩放倍数
    },
    mydata:{
      des:{
        left:0.2,
        top:0.4
      }
    }
  },
  //目标点跳转屏幕中心
  mydes(){
    var that=this
    var left=this.data.mydata.des.left
    var top=this.data.mydata.des.top
    var touch=this.data.touch
    // 目前滑动的位置
    var scroll_h=this.data.scrollHeight
    var scroll_w=this.data.scrollWidth
    /**
		* 0.5*(scroll_w-touch.baseWidth) 滑动的位置到图片中心的距离
		* (left-0.5)*touch.baseWidth*touch.scale 目标点到图片中心的距离
		* 0.5*touch.baseWidth 图片中心 
		* left*touch*baseWidth 目标点在图片位置  
	*/
    that.setData({
      'touch.offsetX':0.5*(scroll_w-touch.baseWidth)-(left-0.5)*touch.baseWidth*touch.scale,
      'touch.offsetY':0.5*(scroll_h-touch.baseHeight)-(top-0.5)*touch.baseHeight*touch.scale,
    })   
},
  //事件处理函数
  touchstartCallback: function(e) {
    //触摸开始
    console.log('touchstartCallback');
    console.log(e);
    if (e.touches.length === 1) {
      let {clientX, clientY} = e.touches[0];
      this.startX = clientX;
      this.startY = clientY;
      this.touchStartEvent = e.touches;
    } else {
      let xMove = e.touches[1].clientX - e.touches[0].clientX;
      let yMove = e.touches[1].clientY - e.touches[0].clientY;
      let distance = Math.sqrt(xMove * xMove + yMove * yMove);
      this.setData({
        'touch.distance': distance,
        'touch.zoom': true, //缩放状态
      })
    }
  },
  touchmoveCallback: function(e) {
    //触摸移动中
    //console.log('touchmoveCallback');
    //console.log(e);
    if (e.touches.length === 1) {
      //单指移动
      if (this.data.touch.zoom) {
        //缩放状态，不处理单指
        return ;
      }
      let {clientX, clientY} = e.touches[0];
      let offsetX = clientX - this.startX;
      let offsetY = clientY- this.startY;
      this.startX = clientX;
      this.startY = clientY;
      let touch = this.data.touch;
      touch.offsetX += offsetX;
      touch.offsetY += offsetY;
      touch.offsetLeftX = -touch.offsetX;
      touch.offsetLeftY = -touch.offsetLeftY;
      this.setData({
        touch: touch
      });
    } else {
      //双指缩放
      let xMove = e.touches[1].clientX - e.touches[0].clientX;
      let yMove = e.touches[1].clientY - e.touches[0].clientY;
      let distance = Math.sqrt(xMove * xMove + yMove * yMove);
      var that = this  
      var scroll_h=this.data.scrollHeight
      var scroll_w=this.data.scrollWidth
      let touch = this.data.touch
      let distanceDiff = distance - this.data.touch.distance;
      let newScale = this.data.touch.scale + 0.005 * distanceDiff;
      let c_w = 0.5*(scroll_w-touch.baseWidth) //缩放的偏移距离
      let c_h = 0.5*(scroll_h-touch.baseHeight)
      if (newScale >= 2) {
        newScale = 2
      }
      if (newScale <= 0.2) {
        newScale = 0.2
      }
      console.log("scale"+newScale)
      // 生成新的偏移量
      let offsetX = (touch.offsetX-c_w)*newScale/touch.scale+c_w
      let offsetY = (touch.offsetY-c_h)*newScale/touch.scale+c_h 
      that.setData({
        'touch.distance': distance,
        'touch.scale': newScale,
        'touch.offsetX':offsetX,
        'touch.offsetY':offsetY
      })
    }

  },
  touchendCallback: function(e) {
    //触摸结束
    //console.log('touchendCallback');
    //console.log(e);
    if (e.touches.length === 0) {
      this.setData({
        'touch.zoom': false, //重置缩放状态
      })
    }
  },
  load: function (e) {
    // bindload 这个api是<image>组件的api类似<img>的onload属性 
    var that =this
    var scroll_h=this.data.scrollHeight
    var scroll_w=this.data.scrollWidth
    var width = e.detail.width
    var height =  e.detail.height
    var scale = scroll_w/width
    var iconscale
    // 合适的放大比例
    if(scale>0.5){
      iconscale=1/scale
    }else{
      iconscale=0.5/scale
    }
    that.setData({
      'touch.baseWidth': width,
      'touch.baseHeight': height,
      'touch.offsetX':0.5*(scroll_w-width),
      'touch.offsetY':0.5*(scroll_h-height),
      'touch.scale':scale,
      "touch.iconscale":iconscale,
      show:true
    });
    //加载图像就把目标点居中
    //this.mydes()
  },
  //点击放大
  imgUp(){
    var that = this
    //console.log("up")    
    var scroll_h=this.data.scrollHeight
    var scroll_w=this.data.scrollWidth
    let touch = this.data.touch
    let newScale = touch.scale+0.2
    let c_w = 0.5*(scroll_w-touch.baseWidth) // 新的x轴偏移量
    let c_h = 0.5*(scroll_h-touch.baseHeight) // 新的y轴偏移量

    //console.log(newScale)
    if (newScale >= 2) {
      newScale = 2
    }
    if (newScale <= 0.2) {
      newScale = 0.2
    }
    let offsetX = (touch.offsetX-c_w)*newScale/touch.scale+c_w
    let offsetY = (touch.offsetY-c_h)*newScale/touch.scale+c_h 
    // 赋值 新的 => 旧的
    that.setData({
      'touch.scale': newScale,
      'touch.offsetX':offsetX,
      'touch.offsetY':offsetY
    })
  },
  //点击缩小
  imgDn(){
    var that = this  
    var scroll_h=this.data.scrollHeight
    var scroll_w=this.data.scrollWidth
    let touch = this.data.touch
    let newScale = touch.scale-0.2
    let c_w = 0.5*(scroll_w-touch.baseWidth)
    let c_h = 0.5*(scroll_h-touch.baseHeight)
    if (newScale >= 2) {
      newScale = 2
    }
    if (newScale <= 0.2) {
      newScale = 0.2
    }
    let offsetX = (touch.offsetX-c_w)*newScale/touch.scale+c_w
    let offsetY = (touch.offsetY-c_h)*newScale/touch.scale+c_h 
    // 赋值 新的 => 旧的 
    that.setData({
      'touch.scale': newScale,
      'touch.offsetX':offsetX,
      'touch.offsetY':offsetY
    })
  },
  onLoad(){
    var that = this
    //获取屏幕宽和高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight,
          scrollWidth:res.windowWidth
        });
      }
    });
  },
})
